<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use app\admin\model\Users as UsersModel;
use app\admin\model\Telauth as TelAuth;
use think\Db;
class Users extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("username")) {
            $map['username'] = ["like", "%" . $this->request->param("username") . "%"];
        }
    }

    public function add(){
    	if($this->request->isAjax()){
    		$UsersModel = new UsersModel;
    		$data = $this->request->except(['id']);
    		$res=$UsersModel->save($data);
    		if($res){
	    		return ajax_return_adv('添加成功');
    		}
    		return ajax_return_adv_error('网络故障，稍后再试');
    	}else{
    		return $this->view->fetch(isset($this->template) ? $this->template : 'edit');
    	}
    }
    public function edit(){
    	$UsersModel =new UsersModel;
        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
            // 更新数据
            $res=$UsersModel->save($data,['id'=>$data['id']]);
            if($res){
	            return ajax_return_adv("编辑成功");
            }
            return ajax_return_adv_error("网络故障，稍后再试");
        } else {
            // 编辑
            $id = $this->request->param('id');
            if (!$id) {
                throw new HttpException(404, "缺少参数ID");
            }
            $vo = $UsersModel->find($id);
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }
            $this->view->assign("vo", $vo);
            return $this->view->fetch();
        }
    }
    public function grant(){
        $TelAuth = new TelAuth;
        if($this->request->isAjax()){
            // 更新
            $data = $this->request->post();
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
          

            if($TelAuth->where('uid',$data['id'])->count()){
                //修改
                $update=[
                    'start_time'=>strtotime($data['start_time']),
                    'end_time'=>strtotime($data['end_time']),
                    'addtime'=>time(),
                    'isdelete'=>$data['status'],
                ];
                $res=$TelAuth->save($update,['uid'=>$data['id']]);
                if($res){
                    return ajax_return_adv("授权成功");
                }
            }else{
                //新增
                $update=[
                    'uid'=>$data['id'],
                    'start_time'=>strtotime($data['start_time']),
                    'end_time'=>strtotime($data['end_time']),
                    'addtime'=>time(),
                    'isdelete'=>$data['status'],
                ];
                $res=$TelAuth->data($update)->save();
                if($res){
                    return ajax_return_adv("授权成功");
                }
            }
            return ajax_return_adv_error("网络故障，稍后再试");
        }else{
            $id=$this->request->param('id');
            $mydata=$TelAuth ->where('uid',$id)->find();
            if($mydata){
                $data=[
                    'id'=>$this->request->param('id'),
                    'start_time'=>date('Y-m-d\TH:i',$mydata['start_time']),
                    'end_time'=>date('Y-m-d\TH:i',$mydata['end_time']),
                    'status'=>$mydata['isdelete'],
                ];

            }else{
                $data=[
                    'id'=>$this->request->param('id'),
                    'start_time'=>date('Y-m-d\TH:i'),
                    'end_time'=>date('Y-m-d\TH:i',time()+7200),
                ];

            }
                        $this->view->assign('vo',$data);
            return $this->view->fetch(); 
        }
       
    }

}
